package view
{
	import com.bit101.components.Text;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.Endian;
	
	import utils.Coursor;
	
	public class Console extends Sprite
	{
		private var first:ConsolePoint;
		private var second:ConsolePoint;
		private var thrid:ConsolePoint;
		private var fourth:ConsolePoint;
		private var target:ConsolePoint;
		private var type:String;
		public var targets:DisplayObject;
		private var coefficient:Number;
		public var change:Boolean = true;
		public var content:Sprite;
		private var _rec:Rectangle;
		private var isShowRatotion:Boolean;
		private var isScale:Boolean;
		private var _select:Boolean;
		public var edit:EditTxt;
		private var maskTarget:DisplayObject;
		public var url:Object;
		public var orgScale:Number;
		public var single:Boolean;
		
		private var lineShape:Shape = new Shape();
		public var ishot:int;
		public function Console(targets:DisplayObject,isShowRatotion:Boolean = true,maskTarget:DisplayObject=null,url:Object=null,isscale:Boolean=true)
		{
			this.isShowRatotion = isShowRatotion;
			this.isScale = isscale;
			this.url = url;
			content = new Sprite();
			this.addChild(content);
			this.targets = targets;
			addBitmap();
			content.addChild(lineShape);
			content.addEventListener(MouseEvent.MOUSE_DOWN,onContentMouseClick);
			addPoint()
			drawLine();
			targets.addEventListener(Event.CHANGE,onChange);
			if(maskTarget){
				targets.mask = maskTarget;
			}
			this.maskTarget = maskTarget;
		}
		public function setMask(ismask:Boolean):void{
			if(ismask){
				if(maskTarget){
					targets.mask = maskTarget;
				}
			}else{
				if(maskTarget){
					targets.mask = null;
				}
			}
		}
		private function onChange(event:Event):void{
			targets.x = - targets.width * 0.5;
			targets.y =  - targets.height * 0.5;
			coefficient = targets.width*0.5;
			first.x = targets.x;
			first.y = targets.y;
			second.x = targets.x+targets.width;
			second.y = targets.y;
			thrid.x = targets.x+targets.width;
			thrid.y = targets.y+targets.height;
			fourth.x = targets.x;
			fourth.y = targets.y+targets.height;
			drawLine();
		}
		
		private function addPoint():void{
			first = new ConsolePoint(content,targets.x,targets.y,onClick,onMosueOver);
			second = new ConsolePoint(content,targets.x+targets.width,targets.y,onClick,onMosueOver);
			thrid = new ConsolePoint(content,targets.x+targets.width,targets.y+targets.height,onClick,onMosueOver);
			fourth = new ConsolePoint(content,targets.x,targets.y+targets.height,onClick,onMosueOver);
		}
		private function addBitmap():void{
			content.addChild(targets);
			targets.x = - targets.width * 0.5;
			targets.y =  - targets.height * 0.5;
			coefficient = targets.width*0.5;
		}
		private function onMosueOver(event:MouseEvent):void{
			if(change == false){
				return;
			}
			var t:ConsolePoint = event.target as ConsolePoint; 
			if(Math.sqrt(t.mouseX*t.mouseX + t.mouseY*t.mouseY) < 4){
				if(this.isScale == false){
					return;
				}
				if(t == first || t == thrid){
					Coursor.getInstance().showCoursor("left");
				}else if(t == second || t == fourth){
					Coursor.getInstance().showCoursor("right");
				}
				type = "scale";
			}else{
				if(this.isShowRatotion == false)
					return;
				type = "rotation";
				Coursor.getInstance().showCoursor("rotation");
			}
		}
		private function onContentMouseClick(event:MouseEvent):void{
			if(event.target is ConsolePoint){
				return;
			}
			type = "move";
			change = false;
			Coursor.getInstance().showCoursor("move");
			this.startDrag();
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		private function onClick(event:MouseEvent):void{
			change = false;
			target = event.target as ConsolePoint;
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		private function onMouseMove(event:MouseEvent):void{
			if(type == "scale")
				runNewPoint();
			else if(type == "rotation")
				runRotation();
			drawLine();
		}
		private function onMouseUp(event:MouseEvent):void{
			if(stage == null){
				return;
			}
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			Coursor.getInstance().removeCoursor();
			this.stopDrag();
			change = true;
		}
		
		public function runNewPoint(sc:Number=-1):void{
			if(targets is TextSprite){
				TextSprite(targets).size = Math.abs(content.mouseX)/2.5;
				return;
			}
			if(sc == -1){
				targets.scaleX = targets.scaleY = Math.abs(content.mouseX)/coefficient;
			}else{
				targets.scaleX = targets.scaleY = sc;
			}
			
			targets.x = - targets.width * 0.5;
			targets.y =  - targets.height * 0.5;
			first.setXY(targets.x,targets.y);
			second.setXY(targets.x+targets.width,targets.y);
			thrid.setXY(targets.x+targets.width,targets.y+targets.height);
			fourth.setXY(targets.x,targets.y+targets.height);
		}
		private function runRotation():void{
			if(target == first || target == thrid){
				var angle:Number = Math.atan((this.mouseY-content.y)/(this.mouseX-content.x)) - Math.atan(targets.height/targets.width);
			}else if(target == second || target == fourth){
				angle = Math.atan((this.mouseY-content.y)/(this.mouseX-content.x)) + Math.atan(targets.height/targets.width);
			}
			
			content.rotation = angle * 180/Math.PI;
		}
		private function drawThis():void{
			graphics.beginFill(0x666666,1);
			graphics.drawRect(0,0,763,10);
			graphics.endFill();
		}
		public function drawLine():void{
			
			lineShape.graphics.clear();
			lineShape.graphics.lineStyle(1,0x000000);
			lineShape.graphics.moveTo(first.x,first.y);
			lineShape.graphics.lineTo(second.x,second.y);
			lineShape.graphics.lineTo(thrid.x,thrid.y);
			lineShape.graphics.lineTo(fourth.x,fourth.y);
			lineShape.graphics.lineTo(first.x,first.y);
			
		}

		public function get select():Boolean
		{
			return _select;
		}

		public function set select(value:Boolean):void
		{
			_select = value;
			lineShape.visible = value;
			first.visible = value;
			second.visible = value;
			thrid.visible = value;
			fourth.visible = value;
			if(edit){
				edit.visible = value;
				edit.console = this;
			}
		}
		
		private var delSprite:Sprite
		public function addDel():void{
			delSprite = new Sprite;
			var txt:TextField = new TextField;
			txt.width = 40;
			txt.height = 20;
			txt.htmlText = "<p align='center'><a href='event:myevent'><font color='#545454'>删除</font></a></p>";
			delSprite.addChild(txt);
			delSprite.graphics.beginFill(0xd3d3d3);
			delSprite.graphics.drawRoundRect(0,0,40,20,5,5);
			delSprite.graphics.endFill();
			this.addChild(delSprite);
			delSprite.visible = false;
			txt.addEventListener(MouseEvent.CLICK,onDel);
			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			content.addEventListener(MouseEvent.CLICK,onContentClick);
		}
		public function onDel(event:MouseEvent=null):void{
			if(this.parent){
				this.parent.removeChild(this);
			}
			if(maskTarget.parent){
				maskTarget.parent.removeChild(maskTarget);
			}
		}
		private function onMouseOver(event:MouseEvent):void{
			delSprite.x = this.content.width/2;
			delSprite.y = this.content.height/2;
			delSprite.visible = true;
		}
		public function hideDel():void{
			if(delSprite)
				delSprite.visible = false;
		}
		private function onContentClick(event:MouseEvent):void{
			delSprite.visible = false;
		}
		
		public function getAllInfo():Object{
			var obj:Object = new Object;
			obj.x = this.x;
			obj.y = this.y;
			if(targets is TextSprite){
				obj.type = 1;
				obj.size = TextSprite(targets).size;
				obj.info = TextSprite(targets).getStr();
			}else{
				obj.type = 2;
				obj.rotation = content.rotation;
				obj.scale = targets.scaleX;
				if(url is String){
					obj.url = url;
				}else if(url is EditItem){
					obj.url = EditItem(url).uploadUrl;
				}
			}
			return obj;
		}
		
		public function getAllInfoString():String{
			var str:String = '';
			str += this.x + "^";
			str += this.y + "^";
			if(targets is TextSprite){
				str += "1^"
				str += TextSprite(targets).size + "^"//size
				str += TextSprite(targets).getStr()//info
			}else{
				str += "2^"
				str += int(content.rotation) + "^"//rotation
				str += targets.scaleX + "^"//scale
				if(url is String){
					str += url + "^"//url
				}else if(url is EditItem){
					str += EditItem(url).bigImgUrl + "^"
				}
				str += this.ishot;
			}
			return str;
		}
		
	}
}